﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Brushes;
using Microsoft.Graphics.Canvas.Effects;
//using PictureSharde;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System.Threading;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    [LastModified("2022-5-14","create GradientMappingPage.xaml")]
    [PageType("cpu","渐变映射")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class GradientMappingPage : Page
    {

        public GradientMappingPage()
        {
            this.InitializeComponent();
            Init();
        }

        LayerBitmapData bitmapData = new LayerBitmapData();

        void Init()
        {
            selectPicture.Click += async (s, e) =>
            {
                await bitmapData.SelectOneBitmapAsync();
                canvas.Invalidate();
            };

            canvas.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);

                if (bitmapData.oneBitmap != null)
                {
                    var element = (FrameworkElement)s;
                    float effectWidth = (float)element.ActualWidth;
                    float effectHeight = (float)element.ActualHeight * 0.7f;
                    float previewWidth = effectWidth *0.5f;
                    float previewHeight = (float)element.ActualHeight * 0.3f;

                    var previewTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    previewTran.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(previewTran, 0, effectHeight);

                    var mappColors = CreateGradientImage();
                    var mappBitmap = CanvasBitmap.CreateFromColors(canvas, mappColors, 256, 1);
                    var mappTran = new Transform2DEffect()
                    {
                        Source = mappBitmap,
                        TransformMatrix = Matrix3x2.CreateScale(previewWidth / 256, previewHeight),
                         InterpolationMode = CanvasImageInterpolation.NearestNeighbor,
                    };
             
                    e.DrawingSession.DrawImage(mappTran, previewWidth, effectHeight);

                    var resultColors = PictureFilter.GradientMappingEffect(bitmapData.oneColors, bitmapData.oneWidth, bitmapData.oneHeight, mappColors);
                    var effectBitmap = CanvasBitmap.CreateFromColors(canvas, resultColors, bitmapData.oneWidth, bitmapData.oneHeight);
                    var effectTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    effectTran.Source = effectBitmap;
                    e.DrawingSession.DrawImage(effectTran);

                }
            };

            GradientMapping.Click += (s, e) =>
            {
                canvas.Invalidate();
            };
        }



        /// <summary>
        /// 创建渐变映射颜色表
        /// </summary>
        Color[] CreateGradientImage()
        {

            Random ran = new Random();

            int count = (int)ran.Next(3, 10);
           var mapColors = new Color[count];
            for (int i = 0; i < count; i++)
            {
                var r =(byte) ran.Next(255);
                var g = (byte)ran.Next(255);
                var b = (byte)ran.Next(255);
                mapColors[i] = Color.FromArgb(255, r, g,b);
            }

            var linearColor = PictureFilter.CreateGradinetMapp(mapColors);
            return linearColor;
           
        }
    }
}
